package com.clothes.act;

import android.content.Intent;
import android.widget.EditText;

import com.clothes.MainActivity;
import com.clothes.R;
import com.clothes.app.BaseUrl;
import com.clothes.app.ClothesApp;
import com.clothes.mdoel.Worker;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {

    @Bind(R.id.et_name)
    EditText etName;
    @Bind(R.id.et_password)
    EditText etPassword;

    @Override
    public void initView() {
        setContentView(R.layout.activity_login);
    }

    @Override
    public void initData() {
        String name = ClothesApp.getInstance().getUsername(this);
        String pwd = ClothesApp.getInstance().getPassWord(this);
        if(!name.equals("")){
            etName.setText(name);
            etPassword.setText(pwd);
        }
    }


    @OnClick(R.id.bt_login)
    public void onViewClicked() {
        login();
    }

    // 登录
    private void login(){
        String name = etName.getText().toString();
        String pwd = etPassword.getText().toString();
        if(name.equals("")){
            showToast("请输入工号");
            return;
        }
        if(pwd.equals("")){
            showToast("请输入密码");
            return;
        }
        final String url = getUrl(name,pwd);
        new Thread(new Runnable() {
            @Override
            public void run() {
                post("",url);
            }
        }).start();

    }

    @Override
    protected void onSuccess(String res) {
        getJson(res);
        //保存用户信息
        ClothesApp.getInstance().setUserName(LoginActivity.this,etName.getText().toString());
        ClothesApp.getInstance().setPassWord(LoginActivity.this,etPassword.getText().toString());
        goToActivity(MainActivity.class,new Intent());
        finish();
        showToast("登录成功");
    }

    private String getUrl(String username, String password){
        String url = BaseUrl.LOGIN;
        String url1 = url.replace("M",username);
        String url2 = url1.replace("N",password);
        return url2;
    }


    private void getJson(String res){
        try {
            Worker worker = new Worker();
            JSONObject object = new JSONObject(res);
            JSONObject obj = object.getJSONObject("data");
            String token = obj.getString("token");
//            ClothesApp.getInstance().setToken(LoginActivity.this,token);
            if(obj.has("auth")){
                JSONObject jsonObject = obj.getJSONObject("auth");
                JSONObject ob = jsonObject.getJSONObject("principal");
                worker.setUuid(ob.getString("uuid"));
                worker.setEmplno(ob.getString("emplno"));
                worker.setTele(ob.getString("tele"));
                worker.setType(ob.getString("type"));
                ClothesApp.getInstance().setWorkerInfo(LoginActivity.this,worker);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
